﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using KaartenLib;
using System.Xml;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        
        private static Login login;
        private static Speler speler;

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            speler = new Speler();
            login = new Login(ref speler);
            login.VisibleChanged += new EventHandler(InzetScherm);
            login.FormClosing += new FormClosingEventHandler(TerminateProgram);
            Application.Run(login);
        }

        public static void InzetScherm(object sender, EventArgs args) {
            if (speler != null && speler is Speler && speler.IsLogged()) {
                InzetFormulier inzet = new InzetFormulier(ref speler);
                if (sender is Login)
                {
                    login.Hide();
                }
                inzet.VisibleChanged += new EventHandler(PlayScreen);
                inzet.FormClosing += new FormClosingEventHandler(TerminateProgram);
                inzet.Show();
            }
        }

        public static void PlayScreen(object sender, EventArgs args) {
            if (speler.IsKlaar() && sender is InzetFormulier && ((InzetFormulier) sender).IsKlaar) {
                ((InzetFormulier)sender).Dispose();
                EnenTwintiger enenTwintiger = new EnenTwintiger(ref speler);
                enenTwintiger.VisibleChanged += new EventHandler(InzetScherm);
                enenTwintiger.FormClosing += new FormClosingEventHandler(TerminateProgram);
            }
        }
        

        public static void TerminateProgram(object sender, EventArgs e) {
            //TODO - Opslaan
            try
            {
                if (speler != null)
                {
                    ToXMLFile();
                }
            }
            catch (ArgumentException ex)
            {
                //Speler is nog niet helemaal geset
            }
            speler = null;
            Application.Exit();
        }

        public static void ToXMLFile() {
            XmlDocument xml = new XmlDocument();
            try
            {
                xml.Load(@"..\..\Resources\spelers.xml");
            }
            catch (System.IO.FileNotFoundException ex) {
                XmlDeclaration declaration = xml.CreateXmlDeclaration("1.0", "utf-8", null);

                XmlElement root = xml.CreateElement("spelers");

                xml.InsertBefore(declaration, xml.DocumentElement);
                xml.AppendChild(root);
            }

            XmlElement sp = speler.toXML(ref xml);
            xml.DocumentElement.AppendChild(sp);

            xml.Save(@"..\..\Resources\spelers.xml");
        }
    }
}
